package com.fc.app.fcapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

import com.fc.app.fcapp.cardview.CardViewActivity;
import com.fc.app.fcapp.customview.CustomViewActivity;
import com.fc.app.fcapp.pages.linechart.ChartActivity;
import com.fc.app.fcapp.pages.overdraw.OverDrawActivity;
import com.fc.app.fcapp.recyclerview.HomeActivity;
import com.fc.app.fcapp.service.ResultService;
import com.fc.app.fcapp.service.Service1;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import rx.Observable;
import rx.Subscriber;


/**
 * Created by fc on 2017/5/31.
 */

public class SplashActivity extends AppCompatActivity {
    private LinearLayout splashLayout;
    private List<String> urlList = new ArrayList<>();
    private final String TAG = "fcurl";
    private Button imageLoader,drawbySelf;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_layout);

        //进程保活
        Intent intent= new Intent(SplashActivity.this, Service1.class);
        startService(intent);

        initView();


        startAnim();
        visitUrl();
        rx();

    }

    private void initView() {
        splashLayout = (LinearLayout) findViewById(R.id.splash_layout);
        imageLoader = (Button) findViewById(R.id.imageloader);
        drawbySelf = (Button) findViewById(R.id.drawbyself);
        imageLoader.setOnClickListener(clickListener);
        drawbySelf.setOnClickListener(clickListener);
    }


    private void rx() {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                Log.i("rx","call");
                subscriber.onNext("fc");
            }
        }).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.i("rx","onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i("rx","onError");
            }

            @Override
            public void onNext(String s) {
                Log.i("rx","onNext");
            }
        });
    }

    private void visitUrl() {
        CoreFuncKt.workerThread(new Function0<Unit>() {
            @Override
            public Unit invoke() {
                for (int i =0;i<20;i++){
                    urlList.add("http://blog.csdn.net/meetings/article/details/78203432");
                    urlList.add("http://blog.csdn.net/meetings/article/details/78203432");

                    urlList.add("http://blog.csdn.net/meetings/article/details/78183074");
                    urlList.add("http://blog.csdn.net/meetings/article/details/78183074");
                }
                for (int i = 0; i < urlList.size(); i++) {
                    try {
                        URL url = new URL(urlList.get(i));
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setConnectTimeout(5 * 1000);
                        urlConnection.connect();
                        if (urlConnection.getResponseCode() == 200) {
                            Log.i(TAG, "visitUrl: success : " + url);
                        } else {
                            Log.i(TAG, "visitUrl: failure : " + url);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        });


    }

    private void startAnim() {
        ScaleAnimation alpha = new ScaleAnimation(0.0f, 1.2f, 0.0f, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        alpha.setDuration(500);
        // 动画结束后，保持动画状态
        alpha.setFillAfter(true);

        // 设置动画监听器
        alpha.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            // 动画结束时回调此方法
            @Override
            public void onAnimationEnd(Animation animation) {
                // 跳转到下一个页面
                jumpNextPage(new Intent(SplashActivity.this, CustomViewActivity.class));
            }


        });

        // 启动动画
        splashLayout.startAnimation(alpha);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.equals(imageLoader)) {
                jumpNextPage(new Intent(SplashActivity.this, ChartActivity.class));
            }
            if (v.equals(drawbySelf)) {
                jumpNextPage(new Intent(SplashActivity.this, ChartActivity.class));
            }
        }
    };

    private void jumpNextPage(Intent intent) {
        startActivity(intent);

    }
}
